//
//  JMRightViewController.m
//  分页控制器
//
//  Created by JackMeng on 16/3/18.
//  Copyright © 2016年 JackMeng. All rights reserved.
//

#import "JMRightViewController.h"
#import "ProvinceDetailController.h"


@interface JMRightViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger currentPage;
@property (nonatomic,strong) UITableView *tableVIew;
@end

@implementation JMRightViewController

static NSString *const ID = @"Cell";

-(NSMutableArray *)dataArray{

    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;

}

- (void)viewDidLoad {
    [super viewDidLoad];
self.title = _string;
    self.view.backgroundColor = [UIColor whiteColor];

    UITableView *tab = [[UITableView alloc] init];
    
    self.tableVIew = tab;
    
    tab.delegate = self;
    
    tab.dataSource = self;
    
    [self.view addSubview:tab];
    
    [tab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top);
        
        make.left.equalTo(self.view.mas_left);
        
        make.right.equalTo(self.view.mas_right);
        
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    
    __weak typeof (self) weakSelf = self;
    
    [self.tableVIew addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
        
        
    }];
    [self.tableVIew headerBeginRefreshing];
    
    [self.tableVIew addFooterWithCallback:^{
        
        [weakSelf loadMore];
        
    }];
}

- (void)loadData{

    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': getJCLibrariesHygl,'service': MemberService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld }'}",userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"msg"];
        
        for (NSDictionary *dic in arr) {
            
            NSDictionary *dict = dic[@"attrs"];
            
            leftModel *leftM = [leftModel mj_objectWithKeyValues:dict];
            
            [self.dataArray addObject:leftM];
            
        }
        
        [self.tableVIew headerEndRefreshing];
        
        [self.tableVIew reloadData];
        
    } failure:^(NSError *error) {
        
    }];


}

- (void)loadMore{
    
    self.currentPage ++;
    
    NSString *json = [NSString stringWithFormat:@"{'action': getJCLibrariesHygl,'service': MemberService, 'userName':'thinkgem','password':'admin', 'vaueJson':'{userid :1,pageNum:%ld }'}",self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        
        NSArray *arr = JSON[@"msg"];
        
        for (NSDictionary *dic in arr) {
            
            NSDictionary *dict = dic[@"attrs"];
            
            leftModel *leftM = [leftModel mj_objectWithKeyValues:dict];
            
            [self.dataArray addObject:leftM];
            
        }
        
        if (self.dataArray.count == 0) {
            
            [self.view makeToast:@"没有最新数据哦"];
        }
        
        
        [self.tableVIew footerEndRefreshing];
        
        [self.tableVIew reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    leftModel *model = self.dataArray[indexPath.row];
    
    leftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [leftTableViewCell WithLeftTableViewCell];
        
    }
    
    cell.model = model;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    leftModel *model = self.dataArray[indexPath.row];
    
    ProvinceDetailController *proVC = [[ProvinceDetailController alloc] init];
    
    proVC.contId = model.ID;
    
    [self.navigationController pushViewController:proVC animated:YES];
    
}

@end
















